/**
 * @BelongsProject: hmall-micro
 * @BelongsPackage: com.hmall.search.listener
 * @Author: 王明天
 * @CreateTime: 2025/2/14 16:38
 * @Description: ItemChangeListener
 */
package com.hmall.search.listener;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.hmall.api.item.dto.ItemDTO;
import com.hmall.common.constants.MQConstants;
import com.hmall.common.utils.BeanUtils;
import com.hmall.search.domain.po.ItemDoc;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
public class ItemChangeListener {

    private final RestHighLevelClient esClient;


    // 监听商品修改消息
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MQConstants.ITEM_UPDATE_QUEUE_NAME, durable = "true"),
            exchange = @Exchange(name = MQConstants.ITEM_EXCHANGE_NAME),
            key = MQConstants.ITEM_UPDATE_ROUTING_KEY))
    public void listenItemUpdate(ItemDTO item) throws Exception {

        log.info("监听到商品修改消息：修改的商品信息为：{}", item);
        if(item.getId()==null){
            return;
        }

        //将修改后的商品信息保存到Elasticsearch中
        ItemDoc itemDoc = BeanUtil.toBean(item, ItemDoc.class);

        //转成json字符串
        String jsonStr = JSON.toJSONString(itemDoc);

        //指定素材库
        IndexRequest request = new IndexRequest("items");

        //设置id
        request.id(itemDoc.getId().toString());

        request.source(jsonStr, XContentType.JSON);

        IndexResponse indexResponse = esClient.index(request, RequestOptions.DEFAULT);


        System.out.println(indexResponse);
    }


    // 监听商品新增消息
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MQConstants.ITEM_ADD_QUEUE_NAME, durable = "true"),
            exchange = @Exchange(name = MQConstants.ITEM_EXCHANGE_NAME),
            key = MQConstants.ITEM_ADD_ROUTING_KEY))
    public void listenItemAdd(ItemDTO item) throws Exception {

        log.info("监听到商品修改消息：修改的商品信息为：{}", item);
        if(item.getId()==null){
            return;
        }

        //把item转成itemDoc
        ItemDoc itemDoc = BeanUtils.copyBean(item, ItemDoc.class);

        //转成json字符串
        String jsonStr = JSON.toJSONString(itemDoc);

        //指定素材库
        IndexRequest request = new IndexRequest("items");

        //设置id
        request.id(itemDoc.getId().toString());

        //设置数据
        request.source(jsonStr, XContentType.JSON);

        //执行索引操作并获取响应
        IndexResponse indexResponse = esClient.index(request, RequestOptions.DEFAULT);

        //记录商品新增消息的日志
        log.info("监听到商品新增消息：新增的商品信息为：{}", indexResponse);;
    }


    // 监听商品删除消息
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MQConstants.ITEM_DELETE_QUEUE_NAME, durable = "true"),
            exchange = @Exchange(name = MQConstants.ITEM_EXCHANGE_NAME),
            key = MQConstants.ITEM_DELETE_ROUTING_KEY))
    public void listenItemDelete(Long id) throws Exception {

        DeleteRequest request = new DeleteRequest("items", id.toString());
        DeleteResponse indexResponse = esClient.delete(request, RequestOptions.DEFAULT);

        //记录商品删除消息的日志
        log.info("监听到商品删除消息：删除的商品信息为：{}", indexResponse);
    }
}
